package game;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 * 
 * @author Mr.张
 * 
 */
public class Snake extends JFrame implements KeyListener {

	enum dir {
		UP, DOWN, RIGHT, LEFT
	};

	dir d = dir.LEFT;

	final int SIZE = 20;

	ArrayList<Body> bodys = new ArrayList<Body>();
	Food f = new Food(15, 15);
	Random r = new Random();

	public class Body {
		public int x, y;

		public Body(int x, int y) {
			this.x = x;
			this.y = y;
		}
	}

	public class Food {
		public int x, y;

		public Food(int x, int y) {
			this.x = x;
			this.y = y;
		}
	}

	public Snake() {
		this.setSize(20 * SIZE, 20 * SIZE);
		this.setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.addKeyListener(this);
		
		System.out.println("food:"+f.x+","+f.y);

		Body b = new Body(7, 7);
		Body b1 = new Body(8, 7);
		Body b2 = new Body(9, 7);
		Body b3 = new Body(10, 7);
		Body b4 = new Body(11, 7);
		bodys.add(b);
		bodys.add(b1);
		bodys.add(b2);
		bodys.add(b3);
		bodys.add(b4);

		while (true) {
			for (int i = bodys.size() - 1; i > 0; i--) {
				bodys.get(i).x = bodys.get(i - 1).x;
				bodys.get(i).y = bodys.get(i - 1).y;
			}

			if(bodys.get(0).x==f.x&&bodys.get(0).y==f.y){
				foodlocation();
				bodys.add(new Body(bodys.get(bodys.size()-1).x, bodys.get(bodys.size()-1).y));
			}

			System.out.println("head:"+bodys.get(0).x+","+bodys.get(0).y);
			switch (d) {
			case LEFT:
				bodys.get(0).x--;
				break;
			case RIGHT:
				bodys.get(0).x++;
				break;
			case UP:
				bodys.get(0).y--;
				break;
			case DOWN:
				bodys.get(0).y++;
				break;
			}

			try {
				Thread.sleep(200);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			if(bodys.get(0).x<0||bodys.get(0).x>19||bodys.get(0).y<1||bodys.get(0).y>19){
				JOptionPane.showMessageDialog(null, "GAME OVER", "Game Over",
						JOptionPane.INFORMATION_MESSAGE);
				System.exit(0);
			}
			repaint();
		}
	}

	public void foodlocation() {
		f.x=r.nextInt(19);
		f.y=r.nextInt(18)+1;
		System.out.println("food:"+f.x+","+f.y);
	}

	public void paint(Graphics g) {
		super.paint(g);
		g.setColor(Color.blue);
		for (int i = 0; i < bodys.size(); i++) {
			g.fillRect(bodys.get(i).x * SIZE, bodys.get(i).y * SIZE, SIZE, SIZE);
		}
		g.setColor(Color.red);
		g.fillRect(f.x * SIZE, f.y * SIZE, SIZE, SIZE);
	}

	public static void main(String[] args) {
		new Snake();
	}

	public void keyPressed(KeyEvent e) {
		int key = e.getKeyCode();
		switch (key) {
		case 37:
			if (d == dir.RIGHT)
				break;
			d = dir.LEFT;
			break;
		case 38:
			if (d == dir.DOWN)
				break;
			d = dir.UP;
			break;
		case 39:
			if (d == dir.LEFT)
				break;
			d = dir.RIGHT;
			break;
		case 40:
			if (d == dir.UP)
				break;
			d = dir.DOWN;
			break;
		}
	}

	public void keyReleased(KeyEvent e) {

	}

	public void keyTyped(KeyEvent e) {

	}

}